/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * WelcomeLoginPane.java
 *
 * Created on 10 janv. 2011, 15:35:26
 */
package com.bash.compta.gui;

import com.bash.compta.data.CreditLevel;
import com.bash.compta.data.Login;
import com.bash.compta.db4o.MagicData;
import com.bash.jcouture.util.swing.MySwingUtilities;
import java.util.List;
import javax.swing.ImageIcon;

/**
 *
 * @author bashizip
 */
public class WelcomeLoginPane extends javax.swing.JPanel {

    private MainForm aThis;
    private Login login;

    /** Creates new form WelcomeLoginPane */
    public WelcomeLoginPane() {
        initComponents();
        //lab_img.setIcon(new ImageIcon(getClass().getResource("/com/bash/jcouture/images/Locker.png")));
        busy.setVisible(false);
    }

    WelcomeLoginPane(MainForm aThis) {
        this();
        this.aThis = aThis;

    }

    void login() {

        new Thread(new Runnable() {

            public void run() {
                jButton1.setEnabled(false);
                busy.setBusy(true);

                if (canLogin(tf_nom.getText(), new String(tf_password.getPassword()))) {

                    aThis.connectedMode(login);
                } else {
                    jButton1.setEnabled(true);
                    busy.setBusy(false);
                }

            }
        }).start();


    }

    private boolean canLogin(String username, String password) {

        List<Login> all = MagicData.getAllObjects(Login.class);

        if (all.isEmpty()) {
            Login def = new Login("Patrick Bashizi","bash", "une", CreditLevel.SUPER,true);
            MagicData.store(def);
            MySwingUtilities.showMessageDialog("Liste d'utilisateurs vide;\nUtiliateur par défaut crée: \nnom: bash, password: une");

            this.login = def;
            return true;
        }

        List<Login> log = MagicData.findLoginObject(username, password);
        if (log.isEmpty()) {

            MySwingUtilities.showMessageDialog("Impossible d'établir la connection:\nLogin ou mot de passe incorrect");
            return false;
        } else {
            this.login = log.get(0);
            return true;
        }
    }

    public Login getLogin() {
        return login;
    }

    public void setLogin(Login login) {
        this.login = login;
    }

    public void stopBusy() {
        synchronized (busy) {
            busy.setBusy(false);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        tf_nom = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        tf_password = new javax.swing.JPasswordField();
        busy = new org.jdesktop.swingx.JXBusyLabel();

        jPanel1.setBackground(new java.awt.Color(204, 204, 204));
        jPanel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 102, 204)));

        jLabel1.setFont(new java.awt.Font("Arial", 1, 14)); // NOI18N
        jLabel1.setText("Utilisateur :");

        jLabel2.setFont(new java.awt.Font("Arial", 1, 14)); // NOI18N
        jLabel2.setText("Mot de passe:");

        tf_nom.setFont(new java.awt.Font("Arial", 1, 14)); // NOI18N
        tf_nom.setForeground(new java.awt.Color(102, 102, 102));
        tf_nom.setText("bash");

        jButton1.setText("Connection ");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        tf_password.setFont(new java.awt.Font("Arial", 1, 14)); // NOI18N
        tf_password.setForeground(new java.awt.Color(102, 102, 102));
        tf_password.setText("une");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap(37, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel1))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(tf_password)
                            .addComponent(tf_nom, javax.swing.GroupLayout.DEFAULT_SIZE, 256, Short.MAX_VALUE))
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addComponent(jButton1)
                        .addGap(108, 108, 108))))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(54, 54, 54)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tf_nom, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(tf_password, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jButton1)
                .addContainerGap(21, Short.MAX_VALUE))
        );

        busy.setText("Connection en cours....");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(220, 220, 220)
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(298, 298, 298)
                        .addComponent(busy, javax.swing.GroupLayout.PREFERRED_SIZE, 165, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(220, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(123, 123, 123)
                .addComponent(busy, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 45, Short.MAX_VALUE)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(180, 180, 180))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
busy.setVisible(true);
        login();


        jButton1.setEnabled(true);
// TODO add your handling code here:
    }//GEN-LAST:event_jButton1ActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private org.jdesktop.swingx.JXBusyLabel busy;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTextField tf_nom;
    private javax.swing.JPasswordField tf_password;
    // End of variables declaration//GEN-END:variables
}
